﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_RegisterCardManager : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {

        }
    }

    protected void g_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        if (e.CommandName == "btnDelete")
        {
            long ID = ShoveEL._Convert.StrToLong(e.Item.Cells[8].Text, -1);
            int Status = ShoveEL._Convert.StrToInt(e.Item.Cells[9].Text, -1);

            if (Status == 1)
            {
                ShoveEL._Web.JavaScript.Alert(this, "不能删除已卖出的注册卡。");

                return;
            }

            try
            {
                int i = DAL.Tables.T_Goods.Delete("[ID]=" + ID);

                if (i >= 0)
                {
                    ShoveEL._Web.JavaScript.Alert(this, "删除成功。");
                    BindData();
                }
            }
            catch
            {
                ShoveEL._Web.JavaScript.Alert(this, "删除失败。");
            }
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("RegisterCardEdit.aspx");
    }

    private void BindData()
    {
        DataTable dt = DAL.Views.V_Goods.Select("", "[TypeID] = 1", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void  btnDownload_Click(object sender, EventArgs e)
    {
        string Path = Server.MapPath("~/UpFiles/Excel/TemplateExcel.xls");

        bool success = DataTableToExcel.DownloadFile(Page.Request, Page.Response, "Card.xls", Path, 1024000);

        if (!success)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "下载文件出错！");

        }
    }

}
